package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.SetmealNameDuplicateException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
@Api(tags = "分类相关接口")
@Slf4j
public class CatgoryController {
    @Autowired
    private CategoryService categoryService;


    @ApiOperation("修改分类")
    @PutMapping("/category")
    @CacheEvict(cacheNames = "categoryCache",allEntries = true)
    public Result updateCategory(@RequestBody Category category) {
        categoryService.updateCategory(category);
        return Result.success();
    }

    @ApiOperation("分类分页查询")
    @GetMapping("/category/page")
    public Result getCategorys(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               String name,
                               Integer type) {
        PageResult pageResult = categoryService.getCategorys(page, pageSize, name, type);
        return Result.success(pageResult);
    }

    @ApiOperation("启用禁用分类")
    @PostMapping("/category/status/{status}")
    @CacheEvict(cacheNames = "categoryCache",allEntries = true)
    public Result startOrStopCategory(@PathVariable Integer status, @RequestParam("id") Long id) {
        categoryService.startOrStopCategory(status, id);
        return Result.success();
    }

    @ApiOperation("根据分类查询")
    @GetMapping("/category/list")
    public Result getCategoryByType(@RequestParam Integer type) {
        List<Category> category = categoryService.getCategoryByType(type);
        return Result.success(category);
    }

    @ApiOperation("删除分类（判断内部是否还有套餐或者菜品）")
    @DeleteMapping("/category")
    @CacheEvict(cacheNames = "categoryCache",allEntries = true)
    public Result deleteCategory(@RequestParam("id") Long id) {
        List<DishVO> dishList = categoryService.getDishByCategoryId(id);
        if (dishList.size()>0) {
            categoryService.deleteCategory(id);
            return Result.success();
        }
        return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
    }

    @ApiOperation("修改套餐")
    @PutMapping("/setmeal")
    @CacheEvict(cacheNames = "setMealCache",key = "#setmeal.id")
    public Result putMeal(@RequestBody SetmealDTO setmeal) throws InterruptedException {
        categoryService.putMeal(setmeal);
        return Result.success();
    }

    @ApiOperation("分页查询套餐")
    @GetMapping("/setmeal/page")
    public Result getMeals(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           Long categoryId,
                           String name,
                           Integer status) {
        PageResult pageResult = categoryService.getMeals(page, pageSize, categoryId, name, status);
        return Result.success(pageResult);
    }

    @ApiOperation("批量删除套餐")
    @DeleteMapping("/setmeal")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteMeal(@RequestParam("ids") List<Long> ids) {
        categoryService.deleteMeal(ids);
        return Result.success();
    }

    @ApiOperation("套餐起售停售")
    @PostMapping("/setmeal/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status, @RequestParam("id") long id) {
        categoryService.startOrStopMeal(status, id);
        return Result.success();
    }

    @ApiOperation("新增套餐")
    @PostMapping("/setmeal")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result addMeal(@RequestBody SetmealDTO setmeal) {
        categoryService.addMeal(setmeal);
        return Result.success();
    }

    @ApiOperation("根据id查询套餐")
    @GetMapping("/setmeal/{id}")
    public Result getMealByid(@PathVariable Long id) {
        SetmealVO setmeal = categoryService.getMealByid(id);
        return Result.success(setmeal);
    }

    @ApiOperation("新增分类")
    @PostMapping("/category")
    @CacheEvict(cacheNames = "categoryCache",allEntries = true)
    public Result addCategory(@RequestBody Category category) {
        categoryService.addCategory(category);
        return Result.success();
    }

    @ApiOperation("菜品分页查询")
    @GetMapping("/dish/page")
    public Result pageDish(Long categoryId,
                           String name,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSie,
                           Integer status) {
        PageResult pageResult = categoryService.pageDish(categoryId, name, page, pageSie, status);
        return Result.success(pageResult);
    }

    @ApiOperation("根据id查询菜品")
    @GetMapping("/dish/{id}")
    public Result getDishById(@PathVariable Long id) {
        DishVO dish = categoryService.getDishById(id);
        return Result.success(dish);
    }

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/dish/list")
    public Result getDishBycategoryId(@RequestParam Long categoryId){
        List<Dish>  dishs = categoryService.getDishBycategoryId(categoryId);
        return Result.success(dishs);
    }

    @ApiOperation("修改菜品")
    @PutMapping("/dish")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result updateDish(@RequestBody DishVO dish) {
        categoryService.updateDish(dish);
        return Result.success();
    }

    @ApiOperation("新增菜品")
    @PostMapping("/dish")
    public Result addDish(@RequestBody DishDTO dish) {
        categoryService.addDish(dish);
        return Result.success();
    }

    @ApiOperation("修改菜品的起售或者停止状态")
    @PostMapping("/dish/status/{status}")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status, @RequestParam("id") Long id) {

        categoryService.startOrStop(status, id);
        return Result.success();
    }

    @ApiOperation("批量删除菜品")
    @DeleteMapping("/dish")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result deleteDish(@RequestParam("ids") List<Long> ids) {
        categoryService.deleteDish(ids);
        return Result.success();
    }

}
